package com.lzw.breezeNovel.WebAdmin.service.impl;

import com.lzw.breezeNovel.WebAdmin.mapper.UserMapper;
import com.lzw.breezeNovel.WebAdmin.service.UserService;
import com.lzw.breezeNovel.WebAdmin.vo.StatisticsVo;
import com.lzw.breezeNovel.WebAdmin.vo.UserInfoVo;
import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.common.utils.PasswordUtil;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.entity.query.novel.NovelPage;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.model.entity.query.SimplePage;
import com.lzw.breezeNovel.model.enums.user.BaseGender;
import com.lzw.breezeNovel.model.enums.user.BaseStatus;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;


@SuppressWarnings("all")
@Service
public class UserServiceImpl implements UserService {

    /**
     * 默认密码
     */
    private static final String password = "123456";


    /**
     * 注入mapper
     */
    @Resource
    private UserMapper userMapper;

    /**
     * 新增用户
     * @param readUser
     */
    @Override
    public void addUser(@NotNull UserInfo readUser) {
        //根据用户的手机号查询用户
        Long count = userMapper.selectByPhone(readUser.getPhone());
        if(count > 0){
            throw new BaseException(ResultCodeEnum.ADIMN_IS_REGISTERED);
        }
        //对密码进行加密
        String password = PasswordUtil.encodePassword(this.password);
        //设置密码
        readUser.setPassword(password);
        //设置创建时间
        readUser.setCreateTime(new Date());
        //设置更新时间
        readUser.setUpdateTime(new Date());
        //设置状态
        readUser.setStatus(BaseStatus.ENABLE);
        //新增用户
        userMapper.insert(readUser);
    }

    /**
     * 分页查询用户信息
     * @param readUser
     * @return
     */
    @Override
    public PaginationResultQuery<UserInfoVo> list(NovelPage pages) {
        //对分页查询的条件进行判断
        Integer page = Math.max(Optional.ofNullable(pages.getPageNo()).orElse(1), 1);
        Integer pageSize = Math.max(Optional.ofNullable(pages.getPageSize()).orElse(10), 10);
        //查询所有的用户
        Long count = userMapper.selectCount();
        //创建分页对象
        SimplePage simplePage = new SimplePage(page, Math.toIntExact(count), pageSize);
        //计算分页
        simplePage.action();
        //设置分页对象
        pages.setSimplePage(simplePage);
        //查询用户信息
        List<UserInfo> userInfos = userMapper.selectByPage(pages);
        //创建一个存放UserInfoVo的集合
        List<UserInfoVo> userInfoVos = null;
        //判断查询到的用户信息是否为空
        if(userInfos == null){
           return new PaginationResultQuery<>(Math.toIntExact(count),pageSize,simplePage.getPageNo(),simplePage.getPageTotal(),userInfoVos);
        }
        //遍历集合
        userInfoVos = userInfos.stream().map(userInfo -> {
            //创建UserInfoVo对象
            UserInfoVo userInfoVo = new UserInfoVo();
            //进行属性的拷贝
            BeanUtils.copyProperties(userInfo,userInfoVo);
            //获取用户性别
            String sex = userInfo.getGender().equals(BaseGender.MALE.getCode()) ? "男" : "女";
            //设置用户性别
            userInfoVo.setGender(sex);
            return userInfoVo;
        }).filter(userInfoVo -> {
            return userInfoVo != null;
        }).collect(toList());
        return new PaginationResultQuery<>(Math.toIntExact(count),pageSize,simplePage.getPageNo(),simplePage.getPageTotal(),userInfoVos);
    }

    /**
     * 修改用户信息
     * @param readUser
     */
    @Override
    public void update(UserInfo readUser) {
        userMapper.update(readUser);
    }

    /**
     * 删除用户
     * @param readUser
     */
    @Override
    public void delete(UserInfo readUser) {
        userMapper.delete(readUser);
    }

    /**
     * 批量删除用户
     * @param ids
     */
    @Override
    public void deleteBatch(Long[] ids) {
        userMapper.deleteBatch(ids);
    }

    @Override
    public StatisticsVo getStatistic() {
        return null;
    }
}
